﻿using System;
using System.IO;
using System.Collections.Generic;

/// <summary>
/// Demo application on how to use FooCore to define a database and query data
/// </summary>
namespace FooApplication
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			// For demonstartion purpose, our cow database is saved into a temp file
			var dbFile = Path.Combine (Path.GetTempPath(), "cowdb.data");

			try {
				// Init the cow db based on a file on temp directory
				using (var db = new CowDatabase (dbFile))
				{
					// Insert some cows into our database..
					db.Insert (new CowModel {
						Id = Guid.Parse ("8872d8ba-e470-440d-aa9b-071822e8053f"),
						Breed = "Golden Retriever",
						Name = "GR1",
						Age = 12,
						DnaData = new byte[] {
							0x3A, 0xAB, 0x7D, 0xBF, 0xDE, 0xF0, 0xDF, 0xE0, 0x45, 0xED, 0xC9, 0x01, 0xB0, 0x87, 0x74, 0xFE, 0xD3, 0x00, 0x62, 0xDB, 0x7B, 0x28, 0x7A, 0xA2, 0xA2, 0x13, 0xAB, 0xB9, 0x98, 0x07, 0x54, 0x88, 0xF0, 0x2A, 0xED, 0x92, 0x72, 0x50, 0x78, 0x90, 0x65, 0xC4, 0x2B, 0x42, 0x86, 0x7E, 0x16, 0x63, 0x39, 0x87, 0x7F, 0x3C, 0x9F, 0xB7, 0x5A, 0x20, 0xEF, 0x0F, 0x73, 0x64, 0xC0, 0x27, 0x6B, 0x01, 0xC2, 0xDD, 0xF2, 0x86, 0x10, 0xB3, 0xFC, 0xCD, 0x46, 0x02, 0x57, 0xC0, 0x34, 0xA7, 0xDC, 0x6D, 
							0xEA, 0xB2, 0xC5, 0x1B, 0x6A, 0xE7, 0x72, 0x40, 0x34, 0x30, 0xBE, 0xC5, 0x6D, 0xAA, 0x81, 0x95, 0xAE, 0x45, 0x19, 0x1E, 0x44, 0xBA, 0x94, 0x80, 0x0A, 0x38, 0x85, 0x1F, 0xD6, 0xCF, 0xB6, 0x35, 0x18, 0xD8, 0xEF, 0xEA, 0x34, 0xB6, 0x31, 0xC1, 0x49, 0xAD, 0xE0, 0xD4, 0xC4, 0x7B, 0xC1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xEA, 0xF2, 0x3A, 0x67, 0x83, 0x7B, 0x7F, 0x9D, 0x56, 0xB2, 0x62, 0x9C, 0xD8, 0xEB, 0x6D, 0x79, 0xF2, 0x39, 0x7E, 0x0F, 0x3A, 0x22, 0xCF, 0x66, 0x66, 0x69, 0x51, 0x2C, 0x12, 0x4B, 0xCA, 0x1A, 0xCE, 0xC5, 0xB0, 0x57, 0x5B, 0xEB, 0x06, 0x0D, 0x28, 0x4F, 0x7D, 0x4D, 0x06, 0x85, 0x95, 0x39, 0x97, 0x6D, 0x30, 0xA6, 0x0E, 0x39, 0x36, 0xCA, 0x27, 0x93, 0x61, 0x3E, 0xAE, 0x55, 0x70, 0xE0, 0x85, 0x18, 0x03, 0x91, 0xED, 0xF8, 0x9A, 0xA9, 
							0xAD, 0x31, 0xC0, 0x28, 0x37, 0x25, 0xB5, 0xF0, 0xF3, 0x1E, 0x53, 0xAF, 0xE7, 0x58, 0x1B, 0x5F, 0x4B, 0xAA, 0xDD, 0x38, 0x28, 0x49, 0x04, 0x58, 0xEF, 0x86, 0x9B, 0x36, 0x23, 0x34, 0xAA, 0x78, 0x23, 0x0F, 0xA4, 0x22, 0x09, 0xBC, 0xF1, 0xFA, 0x2C, 0x92, 0x2E, 0x58, 0x84, 0x79, 0xAE, 0xD7, 0x1C, 0x6C, 0x35, 0xE9, 0xC6, 0x50, 0x07, 0x71, 0x49, 0x2E, 0x04, 0x63, 0xD8, 0x75, 0xD9, 0x81, 0xB3, 0xB2, 0xA2, 0x6C, 0xEF, 0x5B, 0x94, 0x81, 0xD5, 0x41, 0xCE, 0x7C, 0x53, 0xEB, 0xE8, 0xB3
						}
					});
					Console.WriteLine ("Inserted 1 cows");

					db.Insert (new CowModel {
						Id = Guid.Parse ("59ee9033-4ec5-40e0-91a7-6c9ecb6e0465"),
						Breed = "Labrador",
						Name = "L1",
						Age = 18,
						DnaData = new byte[] {
							0x73, 0xF5, 0x93, 0xB1, 0xAC, 0x47, 0x0F, 0xED, 0x9B, 0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, 0x8D, 0x3E, 0x27, 0x8A, 0x6F, 0x7A, 
							0x7B, 0x63, 0x4D, 0x4B, 0x15, 0x5B, 0xC2, 0xE6, 0xB8, 0x67, 0x05, 0x5D, 0xB6, 0x41, 0x89, 0xC8, 0xEC, 0x9F, 0xAE, 0x0C, 0xAF, 0x12, 0x87, 0x2F, 0x82, 0xED, 0x90, 0x91, 0x59, 0x7E, 0x92, 0x37, 0x8D, 0xC0, 0x9F, 0xCD, 0x79, 0xD1, 0x33, 0x54, 0x34, 0x86, 0xBF, 0xEE, 0x27, 0x91, 0x62, 0x37, 0xBA, 0x9A, 0xB2, 0x59, 0x60, 0x50, 0x27, 0xD2, 0x33, 0xA4, 0xAD, 0xC9, 0xEE, 0xD2, 0x6B, 0x65, 0x56, 0x59, 0xAA, 0x80, 0xFF, 0xFA, 0xAE, 0xBB, 0xA7, 0xD2, 0x8F, 0xDE, 0xDF, 0xBC, 0x53, 0x3B
						}
					});
					Console.WriteLine ("Inserted 2 cows");
				}

				// Reconstruct our database again, 
				// to demonstrate that cow data are persistence
				using (var db = new CowDatabase (dbFile))
				{
					// Find a cow by its Id, 
					// This uses the primary index so the query is an ad-hoc query.
					// this should return a Labrador.
					Console.WriteLine ("Found cow by id: " + db.Find (Guid.Parse ("8872d8ba-e470-440d-aa9b-071822e8053f")).ToString());

					// Find cows that belongs to the Labrador breed
					Console.WriteLine ("Labrador cows, 18 years old: " );
					foreach (var row in db.FindBy(breed: "Labrador", age: 18)) {
						Console.WriteLine (row.ToString());
					}
				}
			}
			// Clean up stuff after the demo,
			finally {
				if (File.Exists(dbFile)) {
					File.Delete (dbFile);
					Console.WriteLine ("Deleted main database file");
				}

				if (File.Exists(dbFile + ".pidx")) {
					File.Delete (dbFile + ".pidx");
					Console.WriteLine ("Deleted primary index file");
				}

				if (File.Exists(dbFile + ".sidx")) {
					File.Delete (dbFile + ".sidx");
					Console.WriteLine ("Deleted secondary index file");
				}
			}
		}
	}
}
